package com.gxuwz.medical.web.servlet;

import java.io.IOException;
import java.lang.reflect.Method;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;
import com.gxuwz.medical.domain.menu.*;
import com.gxuwz.medical.domain.role.Role;
import com.gxuwz.medical.domain.user.User;
import com.gxuwz.medical.vo.PageBean;

/**
 * 权限管理控制模块
 * 
 * @author 演示
 * 
 */
public class RightServlet extends BaseServlet {

	@Override
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}

	@Override
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		String url = request.getRequestURI();
		String methodName = url.substring(url.lastIndexOf("/") + 1,
				url.lastIndexOf("."));
		Method method = null;
		try {
			method = getClass().getDeclaredMethod(methodName,
					HttpServletRequest.class, HttpServletResponse.class);
			method.invoke(this, request, response);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void jump(HttpServletRequest request, HttpServletResponse response,
			String path) throws ServletException, IOException, SQLException {
		Menu m = new Menu();
		String page = request.getParameter("page");
		if (page == null)
			page = "1";
		int currentpage = Integer.parseInt(page);
		PageBean pageBean = m.queryAll(currentpage, 5);
		request.setAttribute("pageBean", pageBean);
		request.getRequestDispatcher(path).forward(request, response);
	}

	public void del(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException, SQLException {
		String path = "page/right/list.jsp";
		String menuid = request.getParameter("menuid");
		menuid = new String(menuid.getBytes("ISO-8859-1"), "UTF-8");
		Menu m = new Menu();
		m.delete(menuid);
		jump(request, response, path);
	}

	public void list(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException, SQLException {
		String path = "page/right/list.jsp";
		Menu m = new Menu();
		String page = request.getParameter("page");
		if (page == null)
			page = "1";
		int currentpage = Integer.parseInt(page);
		PageBean pageBean = m.queryAll(currentpage, 5);
		request.setAttribute("pageBean", pageBean);
		request.getRequestDispatcher(path).forward(request, response);
	}

	public void add(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException, SQLException {
		String path = "list.jsp";
		String msg = "添加失败";
		String menuname = request.getParameter("menuname");
		String menupid = request.getParameter("menupid");
		String url = request.getParameter("url");
		Menu m = new Menu();
		m.setMenuname(menuname);
		m.setMenupid(menupid);
		m.setUrl(url);
		m.add();
		jump(request, response, path);

	}

	public void edit(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException, SQLException {
		String path = "list.jsp";
		String menuid = request.getParameter("menuid");
		String menuname = request.getParameter("menuname");
		String menupid = request.getParameter("menupid");
		String url = request.getParameter("url");
		Menu m = new Menu();
		m.setMenuid(menuid);
		m.setMenuname(menuname);
		m.setMenupid(menupid);
		m.setUrl(url);
		m.update();
		jump(request, response, path);
	}

	public void tree(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException, SQLException {
		String roleid = request.getParameter("roleid");
		String resp = "";
		Menu menu = new Menu();
		menu.setMenuid("0");
		MenuTree mt;
		if (roleid != null) {
			roleid = new String(roleid.getBytes("ISO-8859-1"), "UTF-8");
			mt = new MenuTree(menu, roleid);
		} else {
			mt = new MenuTree(menu);
		}
		resp = JSON.toJSONString(mt.getRet());
		response.setCharacterEncoding("utf-8");
		response.getWriter().write(resp);
	}
	
	public void getCurrentMenu(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException, SQLException {
		User currentUser = (User)request.getSession().getAttribute("user");
		String resp="";
		if(currentUser!=null)
			resp = JSON.toJSONString(currentUser.getRoles());
		response.setCharacterEncoding("utf-8");
		response.getWriter().write(resp);
	}

}
